//
//  QTZGuideView.m
//  QTZ
//
//  Created by ski on 2017/10/25.
//  Copyright © 2017年 ski. All rights reserved.
//

#import "QTZGuideView.h"

#define GUIDE_FLAGS                                                      @"guide"


@interface QTZGuideView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *imageArray;

@end

@implementation QTZGuideView

+ (void)showGudieView:(NSArray *)imageArray {
    if(imageArray && imageArray.count > 0) {
        NSFileManager *fmanager=[NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], GUIDE_FLAGS];
        BOOL isHasFile = [fmanager fileExistsAtPath:docDir];
        if(!isHasFile) {
            QTZGuideView *xnGuideView = [[QTZGuideView alloc] init:imageArray];
            [[UIApplication sharedApplication].delegate.window addSubview:xnGuideView];
            [fmanager createFileAtPath:docDir contents:nil attributes:nil];
        }
    }
}

- (instancetype)init:(NSArray *)imageArray {
    self = [super init];
    if(self) [self initThisView:imageArray];
    return self;
}

- (void)initThisView:(NSArray *)imageArray {
    _imageArray = imageArray;
    self.frame = CGRectMake(0, 0, FULL_WIDTH, FULL_HEIGHT);
    
    _scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, FULL_HEIGHT)];
    _scrollView.backgroundColor = QTZ_White;
    _scrollView.contentSize=CGSizeMake(FULL_WIDTH * (_imageArray.count), FULL_HEIGHT);
    _scrollView.pagingEnabled=YES;
    _scrollView.bounces = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.tag=7000;
    _scrollView.delegate = self;
    for (int i = 0; i < imageArray.count; i++) {
        
        UIImageView *img = [[UIImageView alloc] init];
        UIImage *image = [self OriginImage:[UIImage imageNamed:imageArray[i]] scaleToSize:CGSizeMake(FULL_WIDTH, FULL_WIDTH * 2208 / 1242)];
        img.image = image;
        [img sizeToFit];
        img.centerX = FULL_WIDTH / 2;
        img.centerY = FULL_HEIGHT / 2;
        img.left = i * FULL_WIDTH;
        [_scrollView addSubview:img];
        
        if (i == imageArray.count - 1) {
            
            UIButton *logbtn = [[UIButton alloc] initWithFrame:CGRectMake(i*FULL_WIDTH+(FULL_WIDTH - 240)/2, FULL_HEIGHT - 60 - (IPHONEX ? 30 : 0), 240, 40)];
            logbtn.layer.borderWidth = 1;
            logbtn.layer.borderColor = QTZ_White.CGColor;
            logbtn.tag = 9911;
            [logbtn addTarget:self action:@selector(toLogin:) forControlEvents:(UIControlEventTouchUpInside)];
            logbtn.backgroundColor = [UIColor clearColor];
            logbtn.layer.borderColor = Theme_Color.CGColor;
            logbtn.layer.borderWidth = 1;
            logbtn.layer.cornerRadius = 22;
            [logbtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [logbtn setTitleColor:Theme_Color forState:0];
            NSString *title = @"立即体验";
            [logbtn setTitle:title forState:(UIControlStateNormal)];
            [_scrollView addSubview:logbtn];
            
        }
        
        
    }
    [self addSubview:_scrollView];
}

- (UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}

#pragma mark scrollView的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x > 3 * FULL_WIDTH)
    {
        
        scrollView.bounces = NO;
        
    }
    
    
}

-(void)dismissGuideView {
    
    [UIView animateWithDuration:0.6f animations:^{
        self.transform = (CGAffineTransformMakeScale(1.5, 1.5));
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0; //让scrollview 渐变消失
    } completion:^(BOOL finished) {
//        //有手机号
//        if ([JLCFRegisterService shareService].hasPhone) {
//            [[JLCFNavigationService sharedService] openUrl:@"qtz://login?hasNumber=1"];
//        } else {
//            [[JLCFNavigationService sharedService] openUrl:@"qtz://UCPhoneNumber"];
//        }
    }];

}

-(void)toLogin:(UIButton *)btn
{
    
    [UIView animateWithDuration:0.6f animations:^{
        self.transform = (CGAffineTransformMakeScale(1.5, 1.5));
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 0; //让scrollview 渐变消失
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    } ];
}

@end
